﻿using System;
using System.Web;
using System.Web.Mvc;
using Ornament.Web.Mvc;
using Products;
using Products.DataAcccess;
using Products.Databases;
using Products.Databases.DataAccess;

namespace Ornament.MVCWebFrame.Models
{
    public class FieldBinder : BaseModelBinder
    {
        protected override object RequestId(HttpRequestBase request)
        {
            if (request["Id"] == null)
                return null;
            return new Guid(request["Id"]);
        }

        protected override object CreateNewObject(ControllerContext controllerContext,
                                                  ModelBindingContext bindingContext, Type modelType)
        {
            HttpRequestBase request = controllerContext.RequestContext.HttpContext.Request;
            if (request["versionId"] == null)
            {
                throw new ArgumentException("Post data need versionId");
            }
            if (request["tableId"] == null)
            {
                throw new ArgumentException("Post data need tableId");
            }
            ProductVersion productVersion = (new VersionDao()).Get(new Guid(request["versionId"]));
            Table table = (new TableAccess()).Get(new Guid(request["tableId"]));
            return table.CreateField(productVersion);
        }
    }

    public class FieldSnapshotBinder : BaseModelBinder
    {
        protected override object RequestId(HttpRequestBase request)
        {
            if (request["Id"] == null)
                throw new ArgumentException("Need Id parameter in positing.");
            return new Guid(request["Id"]);
        }
        
    }
}